<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>信息流管理</el-breadcrumb-item>
      <el-breadcrumb-item>客户资质管理</el-breadcrumb-item>
    </el-breadcrumb>

    <Container class="u-mb15">
      <!-- 筛选 -->
      <el-form
        inline
        class="m-form--filter"
      >
        <el-form-item>
          <el-input
            class="u-w160"
            size="mini"
            placeholder="请输入客户全称"
            v-model.trim="filter.customerFullName"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            class="u-w160"
            size="mini"
            filterable
            placeholder="请选择关联客户"
            v-model="filter.customerId"
          >
            <el-option
              v-for="item in customerOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            @click="search"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </Container>

    <Container>
      <!-- 表格操作 -->
      <el-form
        inline
        class="m-table--handle"
      >
        <el-form-item>
          <el-button
            type="primary"
            size="mini"
            @click="$router.push({
              name: 'flow-customer-aptitude-detail',
              query: {
                action: 'create'
              }
            })"
          >新增</el-button>
        </el-form-item>
         <el-form-item>
            <ImportTable :importName="importName" :importUrl="importUrl"></ImportTable>
        </el-form-item>
        <el-form-item>
          <el-button
              @click="exportExcel('/manage/api/bus/customer_aptitude/export',requestSearch)"
              size="mini"
            >导出</el-button>
        </el-form-item>
        <el-form-item>
          <el-link type="primary" :underline="false" @click="downloadTemplate">下载导入模版</el-link> 
        </el-form-item>
        <CustomTableColumn :columns.sync="tableColumn" />
      </el-form>
      <!-- 表格 -->
      <el-table
        border
        stripe
        :data="tableData"
      >
        <el-table-column
          align="center"
          v-for="(item, index) in filterTableColumn"
          :label="item.label"
          :key="index"
        >
          <template slot-scope="scope">
            <!-- 其他 -->
            <template>{{scope.row[item.prop]}}</template>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="$router.push({
                name: 'flow-customer-aptitude-detail',
                query: {
                  action: 'edit',
                  id: scope.row.id
                }
              })"
            >编辑</el-button>
            <el-button
              type="text"
              @click="$router.push({
                name: 'flow-customer-aptitude-detail',
                query: {
                  action: 'detail',
                  id: scope.row.id
                }
              })"
            >查看</el-button>
            <!-- <el-button
              type="text"
              @click="removeAptitude(scope.row.id)"
            >删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        :page-sizes="[50, 100, 500, 1000]"
        :page-size="page.pageSize"
        layout="total, prev, pager, next, sizes"
        :total="pageTotal"
        @size-change="handlePagerSizeChange"
        @current-change="handlePagerCurrentChange"
      />
    </Container>
  </div>
</template>

<script>
import Container from "@/components/container";
import CustomTableColumn from "@/components/custom-table-column";
import ImportTable from "@/components/import-table.vue";
import { exportExcel } from '@/utils/fileDown.js';

export default {
  name: "flow-customer-aptitude",
  components: {
    Container,
    CustomTableColumn,
    ImportTable
  },
  data() {
    return {
      // 客户名称
      customerOptions: [],
      importName:'导入',
      importUrl:'/manage/api/bus/customer_aptitude/import_data',
      // 筛选
      filter: {
        customerId: Number(this.$route.query.id) || null,
        customerFullName: ""
      },

      tableData: [],
      tableColumn: [
        {
          label: "客户全称",
          prop: "customerFullName",
          visible: true
        },
        {
          label: "纳税人登记号",
          prop: "taxpayerRegistr",
          visible: true
        },
        {
          label: "开户行",
          prop: "depositBank",
          visible: true
        },
        {
          label: "开户银行账号",
          prop: "bankAccount",
          visible: true
        },
        {
          label: "关联客户名称",
          prop: "customerName",
          visible: true
        }
      ],
      page: {
        pageNo: 1,
        pageSize: 50
      },
      pageTotal: 0
    };
  },
  computed: {
    requestSearch() {
      return {
        ...this.filter,
        ...this.page
      }
    },
    filterTableColumn() {
      return this.tableColumn.filter(item => item.visible);
    }
  },
  methods: {
    exportExcel,
     // 下载模板
    downloadTemplate() {
      location.href = "https://s.55duanzi.com/CMS/客户资质导入表格.xls";
    },
    fetchCustomerOptions() {
      this.$http({
        method: "get",
        url: "/manage/api/bus/customer/get_customer_name",
        params: {
          status: 0
        }
      }).then(res => {
        if (res.data.code === 0) {
          this.customerOptions = [
            {
              id: null,
              name: "全部关联客户"
            }
          ].concat(res.data.data);
          if(this.$route.query.id) {
            this.filter.customerId = Number(this.$route.query.id);
          }
        }
      });
    },
    search() {
      this.page.pageNo = 1;
      this.getTable();
    },
    getTable() {
      this.$http({
        method: "post",
        url: "/manage/api/bus/customer_aptitude/get_list",
        data: {
          ...this.filter,
          ...this.page
        }
      }).then(res => {
        if (res.data.code === 0) {
          let data = res.data.data;
          this.tableData = data.list;
          this.pageTotal = data.listCount;
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    // 删除资质
    removeAptitude(id) {
      this.$confirm("是否删除此账号？删除后不可恢复！", "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http({
            method: "post",
            url: "/manage/api/bus/customer_aptitude/del",
            params: {
              customerAptitudeId: id
            }
          }).then(res => {
            if (res.data.code === 0) {
              this.$message.success("删除成功!");
              this.getTable();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {});
    },
    handlePagerSizeChange(v) {
      this.page.pageSize = v;
      this.getTable();
    },
    handlePagerCurrentChange(v) {
      this.page.pageNo = v;
      this.getTable();
    }
  },
  activated(){
    if(this.$route.query.id){
      this.filter= {
          customerId: Number(this.$route.query.id) || null,
          customerFullName: ""
        }
    }
    this.getTable();
  },
  created() {
    this.fetchCustomerOptions();
  }
};
</script>